package com.situ.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.constants.Constants;
import com.situ.stl.entity.Admin;
import com.situ.stl.entity.Coach;
import com.situ.stl.entity.Course;
import com.situ.stl.mapper.CoachMapper;
import com.situ.stl.mapper.CourseMapper;
import com.situ.stl.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
@Service
public class CoachServiceImpl implements CoachService {
    @Autowired
    private CoachMapper coachMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public List<Coach> select(Coach coach) {
        return coachMapper.select(coach);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Coach coach) {
        if (ObjectUtils.isEmpty(page)) page = 1;
        if (ObjectUtils.isEmpty(limit)) limit = 10;
        // 开启分页查询
        PageHelper.startPage(page, limit);
        List<Coach> coaches = coachMapper.select(coach);
        PageInfo<Coach> pageInfo = new PageInfo<>(coaches);
        return pageInfo;
    }

    @Override
    public int addCoach(Coach coach) {
        //查看手机号是否重复
        Coach dbCoach1 = coachMapper.selectCoachByPhone(coach.getPhone());
        if (!ObjectUtils.isEmpty(dbCoach1)){
            return Constants.PHONE_HAS_ERROR;
        }
        //查看微信是否重复
        Coach dbCoach2 = coachMapper.selectCoachByWechat(coach.getWechat());
        if (!ObjectUtils.isEmpty(dbCoach2)){
            return Constants.WECHAT_HAS_ERROR;
        }
        return coachMapper.insert(coach);
    }

    @Override
    public int modifyCoach(Coach coach) {
        //查看手机号是否重复
        Coach dbCoach1 = coachMapper.selectCoachByPhone(coach.getPhone());
        if (!ObjectUtils.isEmpty(dbCoach1) && coach.getId() != dbCoach1.getId()){
            return Constants.PHONE_HAS_ERROR;
        }
        //查看微信是否重复
        Coach dbCoach2 = coachMapper.selectCoachByWechat(coach.getWechat());
        if (!ObjectUtils.isEmpty(dbCoach2) && coach.getId() != dbCoach2.getId()){
            return Constants.PHONE_HAS_ERROR;
        }
        return coachMapper.modifyCoach(coach);
    }

    @Override
    public int deleteCoach(Integer id) throws Exception {
       Coach coach = coachMapper.selectCoachById(id);
       if (ObjectUtils.isEmpty(coach)){
            throw  new Exception("教练不存在！");
       }
      List<Course> courses = courseMapper.selectByCoachId(id);
        if (!courses.isEmpty()) {
            throw new Exception("该教练还有课程，无法删除！");
        }
            return coachMapper.delete(id);
        }

    @Override
    public List<String> getAllPhoto() {
        ArrayList<String> imageList = new ArrayList<>();
        List<Coach> coachList = coachMapper.getCoachList(null);
        for (Coach coach : coachList) {
            imageList.add(coach.getPhoto());
        }
        return imageList;
    }


}
